package com.dz.Incubator.properties.oss;


import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.dz.Incubator.properties.oss.file.OssFile;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

@Component
public class FileClient {

	@Value("${aliyun.endpoint}")
	private String endpoint;
	@Value("${aliyun.hz.url}")
	private String nodeUrl;

	@Value("${aliyun.sts.appview.accessKeyId}")
	private String accessKeyId;

	@Value("${aliyun.sts.appview.accessKeySecret}")
	private String accessKeySecret;

	public static String domain; //域名
	public static String domainFile;//文件域名
	public static String ossDomainFile;//oss文件域名
	public static String realPicture; //原图
	public static String thumbPicture; // 缩略图
	public static String fileBucket; //文件bucket
	public static String imgBucket; //图片bucket


	private OSSClient client = null;
	private static boolean expired = true;
	static {
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				expired = true;
			}
		};
		Timer timer = new Timer();
		long delay = 0;
		long intevalPeriod = 30 * 60 * 1000;
		timer.scheduleAtFixedRate(task, delay, intevalPeriod);
	}

	private OSSClient getOssClient() throws Exception {
		if (expired || client == null) {
			//通过RAM子账户的方式,配置太麻烦,改为直接使用主账户Key和Secret
//			AssumeRoleResponse response = stsService.assumeOSSObjectRWRole();
//			Credentials credentials = response.getCredentials();
//			client = new OSSClient(endpoint, credentials.getAccessKeyId(), credentials.getAccessKeySecret(),
//					credentials.getSecurityToken());

			//直接使用主账户Key和Secret
			client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			expired = false;
		}
		return client;
	}

	public String put(OssFile file, String subfix) throws Exception {
		String key = null;
		InputStream stream = file.getStream();
		try {
			OSSClient client = getOssClient();
			if (stream != null) {
				String uuid = UUID.randomUUID().toString();
				key = nodeUrl + "/" + file.getDocprefix() + "/" + uuid+"."+subfix;
				file.setLocation(key);
				client.putObject(file.getBucket(), key, stream);
			}
		}catch (IOException e) {
		      e.printStackTrace();
	    } finally {
	      try {
	        if (stream != null) stream.close();
	      } catch (IOException e) {
	        e.printStackTrace();
	      }
	    }
		return key;
	}

	public String putLocation(OssFile file) throws Exception {

		OSSClient client = getOssClient();
		String key = null;
		InputStream stream = file.getStream();
		if (stream != null) {
			String uuid = UUID.randomUUID().toString();
			if (StringUtil.isNullOrEmpty(file.getLocation())) {
				key = file.getDocprefix() + "/" + uuid;
				file.setLocation(key);
			} else {
				key = file.getLocation();
			}
			client.putObject(file.getBucket(), key, stream);
		}
		return key;
	}

	public void delete(OssFile file) throws Exception {
		OSSClient client = getOssClient();
		if (file.getLocation() != null) {
			client.deleteObject(file.getBucket(), file.getLocation());
		}
	}

	public OSSObject get(OssFile file) throws Exception {
		OSSClient client = getOssClient();
		if (file.getLocation() != null) {
			return client.getObject(file.getBucket(), file.getLocation());
		}
		return null;
	}



	public String getDomain() {
		return domain;
	}

	public  String getRealPicture() {
		return realPicture;
	}

	public  String getThumbPicture() {
		return thumbPicture;
	}

	public static String getImgBucket() {
		return imgBucket;
	}

	public static String getFileBucket() {
		return fileBucket;
	}

	@Value("${aliyun.domain}")
	public void setDomain(String domain) {
		FileClient.domain = domain;
	}

	@Value("${aliyun.realPicture}")
	public  void setRealPicture(String realPicture) {
		FileClient.realPicture = realPicture;
	}

	@Value("${aliyun.thumbPicture}")
	public  void setThumbPicture(String thumbPicture) {
		FileClient.thumbPicture = thumbPicture;
	}

	@Value("${aliyun.fileBucket}")
	public void setFileBucket(String fileBucket) {
		FileClient.fileBucket = fileBucket;
	}

	@Value("${aliyun.imgBucket}")
	public void setImgBucket(String imgBucket) {
		FileClient.imgBucket = imgBucket;
	}

	public static String getDomainFile() {
		return domainFile;
	}

	@Value("${aliyun.domainFile}")
	public void setDomainFile(String domainFile) {
		FileClient.domainFile = domainFile;
	}

	public static String getOssDomainFile() {
		return ossDomainFile;
	}

	@Value("${aliyun.oss.domainFile}")
	public void setOssDomainFile(String ossDomainFile) {
		FileClient.ossDomainFile = ossDomainFile;
	}
}
